package msr.frontend;

import java.util.Map;

import msr.service.UserService;

import org.apache.commons.codec.digest.DigestUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String username;
	private String passwd;
	private UserService uService = new UserService();
	Map<String, Object> session = ActionContext.getContext().getSession();

	/**
	 * login execute
	 * 
	 * @author tuvv
	 * @param username
	 * @param passwd
	 * @return message that view in client
	 * */
	public String execute() {
		boolean check = uService.checkLogin(username, passwd);

		if (check) {
			// add into session
			String passMd5 = DigestUtils.md5Hex(this.passwd);
			session.put("username", this.username);
			session.put("passwd", passMd5);
			session.put("logged", "true");
			System.out.println("value of username " + username);
			return SUCCESS;
		} else {
			addActionError(getText("error.login"));
			return ERROR;
		}
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}
}
